package cn.iocoder.yudao.module.picturebook.controller.admin.bookcontext;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.bookcontext.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.bookcontext.BookContextDO;
import cn.iocoder.yudao.module.picturebook.service.bookcontext.BookContextService;

@Tag(name = "绘本系统 - 绘本内容表	")
@RestController
@RequestMapping("/picturebook/book-context")
@Validated
public class BookContextController {

    @Resource
    private BookContextService bookContextService;

    @PostMapping("/create")
    @Operation(summary = "创建绘本内容表	")
    @PreAuthorize("@ss.hasPermission('picturebook:book-context:create')")
    public CommonResult<String> createBookContext(@Valid @RequestBody BookContextSaveReqVO createReqVO) {
        return success(bookContextService.createBookContext(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新绘本内容表	")
    @PreAuthorize("@ss.hasPermission('picturebook:book-context:update')")
    public CommonResult<Boolean> updateBookContext(@Valid @RequestBody BookContextSaveReqVO updateReqVO) {
        bookContextService.updateBookContext(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除绘本内容表	")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:book-context:delete')")
    public CommonResult<Boolean> deleteBookContext(@RequestParam("id") String id) {
        bookContextService.deleteBookContext(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除绘本内容表	")
                @PreAuthorize("@ss.hasPermission('picturebook:book-context:delete')")
    public CommonResult<Boolean> deleteBookContextList(@RequestParam("ids") List<String> ids) {
        bookContextService.deleteBookContextListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得绘本内容表	")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:book-context:query')")
    public CommonResult<BookContextRespVO> getBookContext(@RequestParam("id") String id) {
        BookContextRespVO bookContext = bookContextService.getBookContext(id);
        return success(bookContext);
    }

    @GetMapping("/page")
    @Operation(summary = "获得绘本内容表	分页")
    @PreAuthorize("@ss.hasPermission('picturebook:book-context:query')")
    public CommonResult<PageResult<BookContextRespVO>> getBookContextPage(@Valid BookContextPageReqVO pageReqVO) {
        PageResult<BookContextDO> pageResult = bookContextService.getBookContextPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BookContextRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出绘本内容表	 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:book-context:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBookContextExcel(@Valid BookContextPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BookContextDO> list = bookContextService.getBookContextPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "绘本内容表	.xls", "数据", BookContextRespVO.class,
                        BeanUtils.toBean(list, BookContextRespVO.class));
    }

}